package com.fy.market.persistent.respository.activity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fy.market.activity.model.entity.RaffleActivityAccountDayEntity;
import com.fy.market.activity.repository.IRaffleActivityAccountDayRepository;
import com.fy.market.persistent.mapper.RaffleActivityAccountDayMapper;
import com.fy.market.persistent.po.RaffleActivityAccountDay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 抽奖活动账户表-日次数 仓储实现
 * @author fwq
 * @date 2025-10-11
 */
@Slf4j
@Repository
public class RaffleActivityAccountDayRepository implements IRaffleActivityAccountDayRepository {

    @Resource
    private RaffleActivityAccountDayMapper raffleActivityAccountDayMapper;

    @Override
    public RaffleActivityAccountDayEntity queryRaffleActivityAccountDayByUserIdAndActivityId(Integer userId, Integer activityId, String day) {
        LambdaQueryWrapper<RaffleActivityAccountDay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RaffleActivityAccountDay::getUserId, userId)
                .eq(RaffleActivityAccountDay::getActivityId, activityId)
                .eq(RaffleActivityAccountDay::getDay, day);

        RaffleActivityAccountDay raffleActivityAccountDay = raffleActivityAccountDayMapper.selectOne(queryWrapper);
        if (raffleActivityAccountDay == null) {
            return null;
        }

        return RaffleActivityAccountDayEntity.builder()
                .id(raffleActivityAccountDay.getId())
                .userId(raffleActivityAccountDay.getUserId())
                .activityId(raffleActivityAccountDay.getActivityId())
                .day(raffleActivityAccountDay.getDay())
                .dayCount(raffleActivityAccountDay.getDayCount())
                .dayCountSurplus(raffleActivityAccountDay.getDayCountSurplus())
                .createTime(raffleActivityAccountDay.getCreateTime())
                .updateTime(raffleActivityAccountDay.getUpdateTime())
                .build();
    }

    @Override
    public boolean decrRaffleActivityAccountDayCount(Integer userId, Integer activityId, String day) {
        LambdaUpdateWrapper<RaffleActivityAccountDay> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RaffleActivityAccountDay::getUserId, userId)
                .eq(RaffleActivityAccountDay::getActivityId, activityId)
                .eq(RaffleActivityAccountDay::getDay, day)
                .gt(RaffleActivityAccountDay::getDayCountSurplus, 0)
                .setSql("day_count_surplus = day_count_surplus - 1");

        int updateCount = raffleActivityAccountDayMapper.update(null, updateWrapper);
        return updateCount > 0;
    }

    @Override
    public void createRaffleActivityAccountDay(RaffleActivityAccountDayEntity raffleActivityAccountDayEntity) {
        RaffleActivityAccountDay raffleActivityAccountDay = new RaffleActivityAccountDay();
        raffleActivityAccountDay.setUserId(raffleActivityAccountDayEntity.getUserId());
        raffleActivityAccountDay.setActivityId(raffleActivityAccountDayEntity.getActivityId());
        raffleActivityAccountDay.setDay(raffleActivityAccountDayEntity.getDay());
        raffleActivityAccountDay.setDayCount(raffleActivityAccountDayEntity.getDayCount());
        raffleActivityAccountDay.setDayCountSurplus(raffleActivityAccountDayEntity.getDayCountSurplus());
        raffleActivityAccountDay.setCreateTime(new Date());
        raffleActivityAccountDay.setUpdateTime(new Date());

        raffleActivityAccountDayMapper.insert(raffleActivityAccountDay);
    }

    @Override
    public void updateRaffleActivityAccountDay(RaffleActivityAccountDayEntity raffleActivityAccountDayEntity) {
        RaffleActivityAccountDay raffleActivityAccountDay = new RaffleActivityAccountDay();
        raffleActivityAccountDay.setId(raffleActivityAccountDayEntity.getId());
        raffleActivityAccountDay.setUserId(raffleActivityAccountDayEntity.getUserId());
        raffleActivityAccountDay.setActivityId(raffleActivityAccountDayEntity.getActivityId());
        raffleActivityAccountDay.setDay(raffleActivityAccountDayEntity.getDay());
        raffleActivityAccountDay.setDayCount(raffleActivityAccountDayEntity.getDayCount());
        raffleActivityAccountDay.setDayCountSurplus(raffleActivityAccountDayEntity.getDayCountSurplus());
        raffleActivityAccountDay.setUpdateTime(new Date());

        raffleActivityAccountDayMapper.updateById(raffleActivityAccountDay);
    }
}
